package com.lkd.http.controller;

import com.alibaba.excel.EasyExcel;
import com.lkd.entity.OrderCollectEntity;
import com.lkd.http.viewModel.BillExportDataVO;
import com.lkd.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @GetMapping("/serach/{partnerId}")
    public List<OrderCollectEntity> report(@PathVariable(value = "partnerId") String id,
                                           @RequestParam(value = "pageIndex", required = false, defaultValue = "") Integer pageIndex,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "") Integer pageSize,
                                           @RequestParam(value = "start", required = false, defaultValue = "") String start,
                                           @RequestParam(value = "end", required = false, defaultValue = "") String end,
                                           @RequestParam(value = "nodeName", required = false, defaultValue = "") String nodeName) {
        return reportService.report(id, pageIndex, pageSize, start, end, nodeName);

    }

    @GetMapping("/top12Collect/{partnerId}")
    public List<OrderCollectEntity> top12Collect(@PathVariable(value = "partnerId") String ownerId) {
        return reportService.top12Collect(ownerId);
    }

    @GetMapping("/collectReport/{partnerId}/{start}/{end}")
    public Map<String, List> collectReport(@PathVariable(value = "partnerId") String partnerId,
                                           @PathVariable(value = "start") String start,
                                           @PathVariable(value = "end") String end) {
        return reportService.collectReport(partnerId, start, end);
    }


    /**
     * 数据导出
     */
    @GetMapping("/export/{partnerId}/{start}/{end}")
    public void export(
            HttpServletResponse response,
            @PathVariable Integer partnerId,
            @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
            @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(value = "nodeName", required = false, defaultValue = "") String nodeName) throws IOException {

        var exportData = reportService
                .getList(partnerId, nodeName, start, end).stream().map(item -> {
                    var vo = new BillExportDataVO();
                    vo.setAmount(item.getTotalBill());
                    vo.setDate(Date.from(item.getDate().atTime(0, 0).atZone(ZoneId.systemDefault()).toInstant()));//atZone指定时区
                    vo.setOrderCount(item.getOrderCount());
                    vo.setNodeName(item.getNodeName());
                    return vo;
                }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=bill.xlsx");
        EasyExcel.write(response.getOutputStream(), BillExportDataVO.class).sheet("分账数据").doWrite(exportData);
    }

    @GetMapping("/amountCollect/{type}/{start}/{end}")
    public Map amountCollect(@PathVariable("type") Integer type,
                             @PathVariable("start") String start,
                             @PathVariable("end") String end) {
        return reportService.amountCollect(start,end);
    }

    @GetMapping("/regionCollect/{start}/{end}")
    public Map regionCollect(@PathVariable("start") String start,
                             @PathVariable("end") String end) {
        return reportService.regionCollect(start,end);
    }
}
